#!/usr/bin/python
# -*- coding: utf-8 -*-

#-----------------------------------------------------------------------------
# Name:        
# Purpose:     
#
# Author:      Administrator
#
# Created:     2006-11-29 下午05:15:25
# RCS-ID:      Id:  
# Copyright:   (c) 2006
# Licence:     Free To Use
# 
#-----------------------------------------------------------------------------

from win32com.client import Dispatch
import md5
import pywintypes
import sys
import time
import win32api
import win32con

__MOD_NAME__ = "WOW"
__MINO_MOD_VERSION__ = "$Rev: 678 $"
__MOD_VERSION__ = '0.1.'+__MINO_MOD_VERSION__.split()[1]
__MOD_DATE__ = "$Date: 2006-12-08 03:07:10 +0800 $"

GAME_BASENAME = 'wow.exe'
GAME_ACCT_NAME = ''
GAME_ACCT_PASS= ''
GAME_ACCT_POS_X = 1
GAME_ACCT_POS_Y = 1
GAME_PASS_POS_X = 1
GAME_PASS_POS_Y = 1

GAME_WINDOW_H = 768L
GAME_WINDOW_W = 1024L

GAME_ARGS = '-windowed'
GAME_WINDOW_TITLE = 'World of Warcraft'

def pre_check_display():
#===============================================================================
#    display_setting = win32api.EnumDisplaySettings( None, win32con.ENUM_CURRENT_SETTINGS )
#    pre_display_h = display_setting.PelsHeight
#    pre_display_w = display_setting.PelsWidth
#    pre_display_c = display_setting.BitsPerPel
#    pre_display_f = display_setting.DisplayFrequency
#    
#    if pre_display_h != GAME_WINDOW_H and pre_display_w != GAME_WINDOW_W:
#        print win32api.ChangeDisplaySettings( ( 32, 1024, 768, pre_display_f ), 0 )
#    else:
#        pass
#===============================================================================
    global display_modes
    global devmod
    global pre_display_mode
    display_modes = {}
    n = 0
    while True:
        try:
            devmode = win32api.EnumDisplaySettings ( None, n )
        except pywintypes.error:
            break
        else:
            key = ( 
                   devmode.BitsPerPel, 
                   devmode.PelsWidth, 
                   devmode.PelsHeight, 
                   devmode.DisplayFrequency
                   )
            display_modes[key] = devmode
            n += 1
    
    #get pre display mode
    pre_display_mode = win32api.EnumDisplaySettings( None, win32con.ENUM_CURRENT_SETTINGS )
    
    #check pre display mode if w=1024 and h=768, then skip change display setting
    global pre_w
    global pre_h
    global pre_c
    global pre_f
    pre_w = pre_display_mode.PelsWidth
    pre_h = pre_display_mode.PelsHeight
    pre_c = pre_display_mode.BitsPerPel
    pre_f = pre_display_mode.DisplayFrequency
    
    if pre_w == 1024 and pre_h==768:
        pass
    else:
        # change current to game display mode (1024,768)
        mode_required = ( pre_c, 1024, 768, pre_f )
        devmode = display_modes[mode_required]
        win32api.ChangeDisplaySettings ( devmode, 0 )
    
def post_check_display():
    #mode_required = (32, 1280, 1024, 70)
    #devmode = display_modes[mode_required]
    post_display_mode = win32api.EnumDisplaySettings( None, win32con.ENUM_CURRENT_SETTINGS )
    post_w = post_display_mode.PelsWidth
    post_h = post_display_mode.PelsHeight
    post_c = post_display_mode.BitsPerPel
    post_f = post_display_mode.DisplayFrequency
    if post_w==pre_w and post_h==pre_h:
        pass
    else:
        # change current to game display mode (1024,768)
        mode_required = ( pre_c, pre_w, pre_h, pre_f )
        devmode = display_modes[mode_required]
        win32api.ChangeDisplaySettings ( devmode, 0 )
        
def get_md5_time():
    _current_time = time.time()
    _hash_title = md5.new()
    _hash_title.update( str( _current_time ) )
    return _hash_title.hexdigest().upper()
    
        
def enter_game():
    try:
        AutoItX = Dispatch( "AutoItX3.Control" )
    except :
        print >> sys.stderr, 'Load Dll False'
    
    # Block All Input
    AutoItX.BlockInput( 1 )
    AutoItX.Sleep( 20000 )
    if AutoItX.WinActivate( GAME_WINDOW_TITLE, '' ):
        pass
    else:
        if AutoItX.WinWaitActive( GAME_WINDOW_TITLE, '', 8 ):
            pass
        else:
            # Unblock input
            AutoItX.BlockInput( 0 )
            return False
    
    _pre_title = get_md5_time()
    AutoItX.WinSetTitle( GAME_WINDOW_TITLE, '', _pre_title )
    AutoItX.WinSetState( _pre_title, '', AutoItX.SW_MAXIMIZE )
    AutoItX.Sleep( 5000 )
    AutoItX.MouseMove( 462, 396, 10 )
    AutoItX.MouseClick( "left" )
    AutoItX.Sleep( 1000 )
    AutoItX.Send( GAME_ACCT_NAME )
    AutoItX.Sleep( 1000 )
    AutoItX.MouseMove ( 462, 472, 10 )
    AutoItX.MouseClick( "left" )
    AutoItX.Sleep( 1000 )
    AutoItX.Send( GAME_ACCT_PASS )
    AutoItX.Send( "{ENTER}" )
    AutoItX.Sleep( 10000 )
    
    # Unblock input
    AutoItX.BlockInput( 0 )
    
    return True
    
if __name__ == "__main__":
    #enter_game()
    #pre_check_display()
    #time.sleep( 3 )
    #post_check_display()
    #print get_md5_time()
    pass
